home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP11.TXT < prev    next >
Text File  |  1987-11-21  |  30KB  |  653 lines

  1.  
  2.                       Chapter 11 - Structures and Unions
  3.  
  4.  
  5.                             WHAT IS A STRUCTURE?
  6.  
  7.              A structure is a user defined data type.   You have the
  8.         ability  to  define  a new type of  data  considerably  more
  9.         complex than the types we have been using.  A structure is a
  10.         combination  of  several different previously  defined  data
  11.         types,  including other structures we have defined.  An easy
  12.         to  understand definition is,  a structure is a grouping  of
  13.         related  data in a way convenient to the programmer or  user
  14.         of the program.   The best way to understand a structure  is
  15.         to  look  at  an example,  so if you will load  and  display
  16.         STRUCT1.C, we will do just that.
  17.  
  18.              The  program begins with a structure  definition.   The
  19.         key  word  "struct"  is followed by  some  simple  variables
  20.         between  the  braces,   which  are  the  components  of  the
  21.         structure.   After  the  closing brace,  you will  find  two
  22.         variables listed,  namely "boy",  and "girl".   According to
  23.         the  definition  of a structure,  "boy" is  now  a  variable
  24.         composed of three elements,  "initial",  "age", and "grade".
  25.         Each of the three fields are associated with "boy", and each
  26.         can  store a variable of its respective type.   The variable
  27.         "girl"  is also a variable containing three fields with  the
  28.         same  names  as those of "boy" but  are  actually  different
  29.         variables.  We have therefore defined 6 simple variables.
  30.  
  31.                          A SINGLE COMPOUND VARIABLE
  32.  
  33.              Lets  examine  the  variable "boy"  more  closely.   As
  34.         stated above, each of the three elements of "boy" are simple
  35.         variables  and can be used anywhere in a C program  where  a
  36.         variable of their type can be used.   For example, the "age"
  37.         element  is  an integer variable and can therefore  be  used
  38.         anywhere  in a C program where it is legal to use an integer
  39.         variable,  in calculations, as a counter, in I/O operations,
  40.         etc.   The  only problem we have is defining how to use  the
  41.         simple  variable  "age"  which is a  part  of  the  compound
  42.         variable  "boy".   We  use both names with a  decimal  point
  43.         between  them with the major name first.  Thus "boy.age"  is
  44.         the  complete  variable name for the "age" field  of  "boy".
  45.         This  construct can be used anywhere in a C program that  it
  46.         is desired to refer to this field.   In fact,  it is illegal
  47.         to  use the name "boy" or "age" alone because they are  only
  48.         partial definitions of the complete field.  Alone, the names
  49.         refer to nothing.
  50.  
  51.                      ASSIGNING VALUES TO THE VARIABLES
  52.  
  53.              Using  the above definition,  we can assign a value  to
  54.         each  of  the  three fields of "boy" and each of  the  three
  55.         fields  of  "girl".   Note carefully that  "boy.initial"  is
  56.  
  57.  
  58.                                   Page 77
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                       Chapter 11 - Structures and Unions
  69.  
  70.  
  71.         actually  a "char" type variable,  because it  was  assigned
  72.         that in the structure, so it must be assigned a character of
  73.         data.   Notice  that "boy.initial" is assigned the character
  74.         'R'  in agreement with the above rules.   The remaining  two
  75.         fields of "boy" are assigned values in accordance with their
  76.         respective  types.   Finally  the three fields of  girl  are
  77.         assigned values but in a different order to illustrate  that
  78.         the order of assignment is not critical.
  79.  
  80.                      HOW DO WE USE THE RESULTING DATA?
  81.  
  82.              Now  that  we  have assigned values to the  six  simple
  83.         variables, we can do anything we desire with them.  In order
  84.         to keep this first example simple,  we will simply print out
  85.         the values to see if they really do exist as  assigned.   If
  86.         you carefully inspect the "printf" statements,  you will see
  87.         that there is nothing special about them.  The compound name
  88.         of each variable is specified because that is the only valid
  89.         name by which we can refer to these variables.
  90.  
  91.              Structures  are  a very useful method of grouping  data
  92.         together  in  order to make a program easier  to  write  and
  93.         understand.   This  first example is too simple to give  you
  94.         even  a hint of the value of using structures,  but continue
  95.         on  through  these lessons and eventually you will  see  the
  96.         value of using structures.
  97.  
  98.              Compile and run STRUCT1.C and observe the output.
  99.  
  100.                            AN ARRAY OF STRUCTURES
  101.  
  102.              Load  and  display the next  program  named  STRUCT2.C.
  103.         This  program  contains  the same  structure  definition  as
  104.         before  but  this  time we define an array of  12  variables
  105.         named "kids".   This program therefore contains 12 times 3 =
  106.         36  simple variables,  each of which can store one  item  of
  107.         data  provided  that  it is of the correct  type.   We  also
  108.         define a simple variable named "index" for use in the  "for"
  109.         loops.
  110.  
  111.              In order to assign each of the fields a value, we use a
  112.         "for"  loop  and  each  pass through  the  loop  results  in
  113.         assigning a value to three of the fields.  One pass  through
  114.         the  loop assigns all of the values for one of  the  "kids".
  115.         This would not be a very useful way to assign data in a real
  116.         situation, but a loop could read the data in from a file and
  117.         store it in the correct fields.  You might consider this the
  118.         crude beginning of a data base, which it is.
  119.  
  120.              In  the next few instructions of the program we  assign
  121.         new  values to some of the fields to illustrate  the  method
  122.  
  123.  
  124.                                   Page 78
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                       Chapter 11 - Structures and Unions
  135.  
  136.  
  137.         used to accomplish this.  It should be self explanatory,  so
  138.         no additional comments will be given.
  139.  
  140.                        A RECENT UPGRADE TO THE C LANGUAGE
  141.  
  142.              Most  modern  C  compilers will allow you  to  copy  an
  143.         entire structure with one statement. This is a fairly recent
  144.         addition  to the C language and will be a part of  the  ANSI
  145.         standard  when it is published, so you should feel  free  to
  146.         use it with your C compiler if it is available.  Line 22  is
  147.         an  example  of  using  a  structure  assignment.   In  this
  148.         statement,  all  3 fields of kids[4] are copied  into  their
  149.         respective fields of kids[10].
  150.  
  151.                    WE FINALLY DISPLAY ALL OF THE RESULTS
  152.  
  153.              The  last few statements contain a "for" loop in  which
  154.         all  of  the generated values are displayed in  a  formatted
  155.         list.   Compile and run the program to see if it  does  what
  156.         you expect it to do.
  157.  
  158.                    USING POINTERS AND STRUCTURES TOGETHER
  159.  
  160.              Load  and  display  the  file named  STRUCT3.C  for  an
  161.         example of using pointers with structures.   This program is
  162.         identical  to the last program except that it uses  pointers
  163.         for some of the operations.
  164.  
  165.              The  first  difference shows up in  the  definition  of
  166.         variables  following  the  structure  definition.   In  this
  167.         program  we define a pointer named "point" which is  defined
  168.         as  a  pointer that points to the structure.   It  would  be
  169.         illegal  to  try to use this pointer to point to  any  other
  170.         variable  type.   There  is a very definite reason for  this
  171.         restriction  in  C as we have alluded to  earlier  and  will
  172.         review in the next few paragraphs.
  173.  
  174.              The  next difference is in the "for" loop where we  use
  175.         the pointer for accessing the data fields.  Since